<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>发送消息</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <script type="text/javascript" charset="utf-8" src="${pageContext.request.contextPath}/resource/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="${pageContext.request.contextPath}/resource/ueditor/ueditor.all.min.js"> </script>
    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8" src="${pageContext.request.contextPath}/resource/ueditor/lang/zh-cn/zh-cn.js"></script>
	<%@ include file="common/head.jspf" %>
    <style type="text/css">
        div{
            width:100%;
        }
        .background{
        	width:80%;
        	height:40px;
        	line-height:40px;
        	background:#E8E2E2;
        }
        .textMessage{
       		width:80%;
        	border:1px solid #CAC5C5;
        }
        .imageMessage{
        	width:80%;
        	border:1px solid #CAC5C5;
        }
    </style>
</head>
<body>
	<h5>发送消息</h5>
	<div class="background">&nbsp;&nbsp;<span style="cursor:pointer" id="textMessage">文字</span> &nbsp;<span style="cursor:pointer" id="imageMessage">图片</span></div>
	<!-- 文本消息编辑器 -->
	<div class="textMessage">	
		<div>
		    <script id="editor" type="text/plain" style="width:80%;height:300px;margin:20px;"></script>
		</div>
		<div style="margin:20px;">
		    <div>
		        <button onclick="getContent()">发送</button>
		        <a href="javascript:history.go(-1);"><button >返回</button></a>
			</div>
		</div>
	</div>
	<!-- 图片消息编辑器 -->
	<div class="imageMessage" style="display:none;">	
		<div>
		    <script id="editorImage" type="text/plain" style="width:80%;height:300px;margin:20px;"></script>
		</div>
		<div style="margin:20px;">
		    <div>
		        <button onclick="getContentImage()">发送</button>
		        <a href="javascript:history.go(-1);"><button >返回</button></a>
			</div>
		</div>
	</div>

<script type="text/javascript">

	
	$(function(){
		//实例化编辑器(文本)
	    var ue = UE.getEditor('editor',{toolbars: [
	                                                [
	                                                 'italic', //斜体
	                                                 'underline', //下划线
	                                                 'strikethrough', //删除线
	                                                 'pasteplain', //纯文本粘贴模式
	                                                 'selectall', //全选
	                                                 'horizontal', //分隔线
	                                                 'removeformat', //清除格式
	                                                 'time', //时间
	                                                 'date', //日期
	                                                 'spechars', //特殊字符
	                                                 'cleardoc', //清空文档
	                                                 'paragraph', //段落格式
	                                             ]
	                                         ]});
		
	  //实例化编辑器(图像)
	    var ue = UE.getEditor('editorImage',{toolbars: [
	                                                [
	                                                 'snapscreen', //截图
	                                                 'simpleupload', //单图上传
	                                                 'insertimage', //多图上传
	                                             ]
	                                         ]});
	  //实例化点击事件
	  $("#imageMessage").click(function(){
		  //div是否隐藏
		  if($(".textMessage").is(":visible")){
			  $(".textMessage").hide();
			  $(".imageMessage").show();
		  }
	  });
      	
	  //实例化点击事件
	  $("#textMessage").click(function(){
		  //div是否隐藏
		  if($(".imageMessage").is(":visible")){
			  $(".imageMessage").hide();
			  $(".textMessage").show();
		  }
	  });
	});

    //发送文本消息
    function getContent() {
        //获取编辑的纯文本
    	var text = UE.getEditor('editor').getContentTxt();
        
        //获取openid
        var openid = "${openid}";
       	
        $.post("${pageContext.request.contextPath}/audience/sendMessage.do",{'openid':openid,'text':text},function(data){
        	var result = eval("("+ data +")");
        	if(result.errcode == 0){
        		alert("发送成功");
        	}
        	if(result.errcode == 45015){
        		alert("回复时间超过限制");
        	}
		},"text");
    }
    
    
    //发送图片消息
    function getContentImage() {
        //获取编辑的html
    	var text = UE.getEditor('editorImage').getContent();
        //获取openid
        var openid = "${openid}";
       	
        $.post("${pageContext.request.contextPath}/audience/sendImageMessage.do",{'openid':openid,'text':text},function(data){
        	var result = eval("("+ data +")");
        	if(result.errcode == 0){
        		alert("发送成功");
        	}
        	if(result.errcode == 45015){
        		alert("回复时间超过限制");
        	}
		},"text");
    
    }
    
</script>
</body>
</html>